import turtle
t0=turtle.Turtle()
t1=turtle.Turtle()
t0.speed = 0
t1.speed = 2

t1.width(2)
t0.hideturtle()
t1.hideturtle()

t0.color("red")

temp = 0
x = 0
y = 0
tempx=0
tempy=0
F=0
i=0
xline=[10,-20, 40, 70,-110, 160]
yline=[ 5, 30,-50, 70,  10,-150]

def init():
        global t0
        t0.up()
        t0.goto(160*2,200*2)
        t0.down()
        t0.goto(160*2,0)
        t0.goto(0,0)


def draw(nx , ny):
	global x
	global y
	global t0
	t0.up()
	t0.goto(x,y)
	t0.down()
	t0.goto(nx,ny)
	t0.up()

def draw2(x,y,nx,ny):
    global t0
    t0.up()
    t0.goto(x,y)
    t0.down()
    t0.color("blue")
    t0.goto(nx,ny)
    t0.up()
    t0.color("red")

def G0(xin , yin):
        global i
        print("G0:Start.......x=%d ,y=%d"%(xline[i],yline[i]))
        global x
        global y
        global t1
        if(xin>=x):
                xdir=1
                tempx=xin-x
        else:
                xdir=-1
                tempx=x-xin
        if(yin>=y):
                ydir=1
                tempy=yin-y
        else:
                ydir=-1
                tempy=y-yin
        if(tempx>=tempy):
                temp=tempy
        else:
                temp=tempx
        draw(xin,yin)
        t1.up()
        t1.goto(x,y)
        t1.down()
        temptemp=temp
        while(temptemp>0):
                t1.goto(xdir+x,xdir+y)
                x+=xdir
                y+=ydir
                temptemp-=1
        while(tempx-temp>0):
                x+=xdir
                t1.goto(x,y)
                tempx-=1
        while(tempy-temp>0):
                y+=ydir
                t1.goto(x,y)
                tempy-=1
        print("........G0:done")
	
def G1(xin , yin):
        global i
        print("G1:Start.......x=%d ,y=%d"%(xline[i],yline[i]))
        global x
        global y
        global t1
        if(xin>=x):
                xdir=1
                tempx = xin-x
        else:
                xdir=-1
                tempx = x-xin

        if(yin>=y):
                ydir=1
                tempy = yin-y
        else:
                ydir=-1
                tempy = y-yin
        
        temp=tempx+tempy
        draw(xin,yin)
        t1.up()
        t1.goto(x,y)
        t1.down()
        F=0
        while (temp>0):
                if F>=0:
                        t1.goto(xdir+x,y)
                        x+=xdir
                        F=F-tempy
                else:
                        t1.goto(x,ydir+y)
                        y+=ydir
                        F=F+tempx
                temp-=1
        x=xin
        y=yin
        print("........G1:done")

def G2(ox,oy,x0,y0,x1,y1):
    draw2(x0,y0,x1,y1)
    global t1
    global x
    global y
    t1.up()
    t1.goto(x0,y0)
    t1.down()
    x0-=ox
    x1-=ox
    y0-=oy
    y1-=oy
    F=0
    if x0<=x1 and x0>=0 and y0>=y1 and y1>=0:   #1
        tempx=x1-x0
        tempy=y0-y1
        xdir=1
        ydir=-1
        xpm=2
        ypm=-2
        state=1
    if x0<=x1 and x1<=0 and y0<=y1 and y0>=0:   #2
        tempx=x1-x0
        tempy=y1-y0
        xdir=1
        ydir=1
        xpm=2
        ypm=2
        state=2
    if x0>=x1 and x0<=0 and y0<=y1 and y1<=0:   #3
        tempx=x0-x1
        tempy=y1-y0
        xdir=-1
        ydir=1
        xpm=-2
        ypm=2
        state=1
    if x0>=x1 and x1>=0 and y0>=y1 and y0<=0:   #4
        tempx=x0-x1
        tempy=y0-y1
        xdir=-1
        ydir=-1
        xpm=-2
        ypm=-2
        state=2
    temp=tempx+tempy
    while (temp>0):
        if(state==1):
            if(F>=0):
                y0+=ydir
                F=F+ypm*y0+1
            else:
                x0+=xdir
                F=F+xpm*x0
        elif(state==2):
            if(F>=0):
                x0+=xdir
                F=F+xpm*x0+1
            else:
                y0+=ydir
                F=F+ypm*y0+1
        t1.goto(x0+ox,y0+oy)
        temp-=1
        x=x0+ox
        y=y0+oy
        print("%d %d %d"%(temp,tempx,tempy))

def G3(ox,oy,x0,y0,x1,y1):
    draw2(x0,y0,x1,y1)
    global t1
    t1.up()
    t1.goto(x0,y0)
    t1.down()
    global x
    global y
    x=x0
    y=y0
    x0-=ox
    x1-=ox
    y0-=oy
    y1-=oy
    F=0
    if x0>=x1 and x1>=0 and y0<=y1 and y0>=0:   #1
        tempx=x0-x1
        tempy=y1-y0
        xdir=-1
        ydir=1
        xpm=-2
        ypm=2
        state=1
    if x0>=x1 and x0<=0 and y0>=y1 and y1>=0:   #2
        tempx=x0-x1
        tempy=y0-y1
        xdir=-1
        ydir=-1
        xpm=-2
        ypm=-2
        state=2
    if x0<=x1 and x1<=0 and y0>=y1 and y0<=0:   #3
        tempx=x1-x0
        tempy=y0-y1
        xdir=1
        ydir=-1
        xpm=2
        ypm=-2
        state=1
    if x0<=x1 and x0>=0 and y0<=y1 and y1<=0:   #4
        tempx=x1-x0
        tempy=y1-y0
        xdir=1
        ydir=1
        xpm=2
        ypm=2
        state=2
    temp=tempx+tempy
    while (temp>0):
        if(state==1):
            if(F>=0):
                x0+=xdir
                F=F+xpm*x0+1
            else:
                y0+=ydir
                F=F+ypm*y0
        elif(state==2):
            if(F>=0):
                y0+=ydir
                F=F+ypm*y0+1
            else:
                x0+=xdir
                F=F+xpm*x0+1
        t1.goto(x0+ox,y0+oy)
        x=x0+ox
        y=y0+oy
        temp-=1
        print("%d"%(F))
        


def test():
        global i
        while (i<len(xline)): 
                G0(xline[i]*2,yline[i]*2)
                i+=1
        print("All done!")

#test()
G3(0,0,200,0,0,200)
t1.color("red")
G2(0,0,0,200,200,0)
t1.color("black")
G1(100,30)
G0(-27,65)
